<!-- TODO -->
<template>
    <div>
		<el-card class="!border-none" shadow="never">
			<el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
				<el-form-item label="通知标题">
					<el-input
							class="w-[280px]"
							v-model="queryParams.name"
							placeholder="请输入通知标题"
							clearable
							@keyup.enter="resetPage"
					/>
				</el-form-item>
<!--				<el-form-item label="使用状态">-->
<!--					<el-select v-model="queryParams.status" clearable>-->
<!--						<template v-for="item in statusList">-->
<!--							<el-option :value="item.id" :label="item.name">{{item.name}}</el-option>-->
<!--						</template>-->
<!--					</el-select>-->
<!--				</el-form-item>-->
<!--				<el-form-item label="领取时间">-->
<!--					<daterange-picker-->
<!--							value-format="YYYY-MM-DD"-->
<!--							v-model:startTime="queryParams.start_time"-->
<!--							v-model:endTime="queryParams.end_time"-->
<!--					/>-->
<!--				</el-form-item>-->
				<el-form-item>
					<el-button type="primary" @click="resetPage">查询</el-button>
					<el-button @click="resetParams">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
        <el-card class="!border-none mt-4" shadow="never">
           <el-button class="mb-4" v-perms="['auth.admin/add']" type="primary" @click="handleAdd" style="float: right">
                添加通知
            </el-button>
<!--			<el-button class="mb-4" v-perms="['auth.admin/add']" type="primary" @click="handleAdd">-->
<!--				一键删除-->
<!--			</el-button>-->
            <el-table size="large" :data="pager.lists"   @selection-change="handleSelectionChange">
<!--				<el-table-column type="selection" width="55" />-->
<!--				<el-table-column label="排序权重" prop="id" min-width="100">-->
<!--				    <template #default="{ row }">-->
<!--				       <el-input-->
<!--							   class="w-[100px]"-->
<!--							v-model="queryParams.user_keyword"-->
<!--							@keyup.enter="resetPage"-->
<!--						/>-->
<!--				    </template>-->
<!--				</el-table-column>-->
                <el-table-column label="通知标题" prop="name" min-width="200" >
                    <template #default="{ row }">
                        {{ row.name }}
                    </template>
                </el-table-column>
				<el-table-column label="阅读次数" prop="num_read" min-width="120" >
					<template #default="{ row }">
						{{ row.num_read }}
					</template>
				</el-table-column>
				<el-table-column label="通知状态" prop="name" min-width="120" >
					<template #default="{ row }">
						<el-switch
							size="large"
							v-model="row.status"
							:active-value="1"
							:inactive-value="0"
							width="80px"
							inline-prompt
							style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
							active-text="正常"
							inactive-text="隐藏"
							@click="toggleDark(row)"
						/>
					</template>
				</el-table-column>
				<el-table-column label="创建时间" prop="create_at" min-width="180" />
                <el-table-column label="操作" fixed="right" min-width="160">
                    <template #default="{ row }">
                        <el-button type="primary" @click="handleEdit(row)"> 编辑 </el-button>
                        <el-button type="danger" @click="handleDelete(row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup>
import { usePaging } from '@/hooks/usePaging'
import {messageList, stateMessage, decMessage, stateEntrance} from '@/api/reception'
import EditPopup from './edit.vue'
import feedback from '@/utils/feedback'
import {ref} from "vue";

const queryParams = reactive({
	name: '',
	// status: '',
	// created_at: '',
});

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: messageList,
	params: queryParams
})
getLists()


//删除
const handleDelete = async (id : any) => {
    await feedback.confirm('确定要删除该通知？')
    await decMessage({ message_id: id})
    getLists()
}

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (row:any) => {
	showEdit.value = true
	await nextTick()
	editRef.value?.open('edit')
	editRef.value?.getDetail(row)
}


const ids = ref<any[]>([])
const handleSelectionChange = (val: any[]) => {
	ids.value = val.map((item) => {
		return item.id
	})
}

const toggleDark = (row: any) => {
	//1 显示 0 不显示
	stateMessage({message_id: row.id, status: row.status})
}

</script>
<style lang="scss" scope>
//TODO
.el-alert--warning.is-light {
    background-color: rgba(246, 248, 255, 1);
    color: rgba(64, 115, 250, 1);
}
</style>
